package com.njmaomao.gj.pyot.controller;

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.njmaomao.gj.common.util.BeanUtils;
import com.njmaomao.gj.pyot.bean.yto.ExpressOrderRequest;
import com.njmaomao.gj.pyot.bean.yto.ExpressOrderBean;
import com.njmaomao.gj.pyot.common.bean.Response;
import com.njmaomao.gj.pyot.common.bean.dto.yto.MianRequestOrderDTO;
import com.njmaomao.gj.pyot.common.bean.dto.yto.RequestOrderDTO;
import com.njmaomao.gj.pyot.common.bean.dto.yto.TransportInfoReqDTO;
import com.njmaomao.gj.pyot.common.bean.dto.yto.UpdateInfoDTO;
import com.njmaomao.gj.pyot.common.bean.dto.yto.WaybillQueryReqDTO;
import com.njmaomao.gj.pyot.domain.yto.ChargeQueryResponse;
import com.njmaomao.gj.pyot.domain.yto.MianResponse;
import com.njmaomao.gj.pyot.domain.yto.WaybillQueryResponse;
import com.njmaomao.gj.pyot.service.YtoAppService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(description = "圆通接口")
@RestController
@RequestMapping("/yto")
public class YtoController {

    @Autowired
    private YtoAppService ytoAppService;

    @ApiOperation(value = "订单创建接口", notes = "客户端发起请求后，本接口接收订单信息HTTP请求，解析订单信息XML，并存入圆通系统，下单成功后业务员会在一定时间内带上传统面单上门揽")
    @RequestMapping(value = "/order-create", method = {RequestMethod.POST}, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Response<com.njmaomao.gj.pyot.domain.yto.Response> orderCreate(@RequestBody @Valid RequestOrderDTO req) {
        return Response.success(req, ytoAppService.orderCreate(req));
    }

    @ApiOperation(value = "订单取消接口", notes = "通过调用该接口，商户可以在创建订单成功后取消相应订单，订单在【已揽收】状态以及其后状态，调用此接口无效。")
    @RequestMapping(value = "/order-cancel", method = {RequestMethod.POST}, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Response<com.njmaomao.gj.pyot.domain.yto.Response> orderCancel(@RequestBody @Valid UpdateInfoDTO req) {
        return Response.success(req, ytoAppService.orderCancel(req));
    }

    @ApiOperation(value = "电子面单下单接口", notes = "客户端发起请求后，圆通电子面单接口在http response里把电子面单信息同步给客户端。 ")
    @RequestMapping(value = "/e-order-create", method = {RequestMethod.POST})
    public Response<ExpressOrderBean> eorderCreate(@RequestBody @Valid ExpressOrderRequest request) {
    	MianRequestOrderDTO req = new MianRequestOrderDTO();
    	BeanUtils.copyProperties(req, request);

    	Date now = new Date();
    	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    	req.setClientID("K21000119");
    	req.setLogisticProviderID("YTO");
    	req.setCustomerId("K21000119");
    	req.setTxLogisticID("WP15183117908113");
    	req.setTradeNo("K21000119");
    	req.setTotalServiceFee("1");
    	req.setCodSplitFee("1");
    	req.setOrderType("1");
    	req.setServiceType("1");
    	req.setFlag("1");
    	req.setSendStartTime(sdf.format(now));
    	req.setSendEndTime(sdf.format(now));
    	req.setGoodsValue("1");
    	req.setInsuranceValue("1");
    	req.setSpecial("1");
    	req.setRemark("1");
    		
        MianResponse resp = ytoAppService.eorderCreate(req);
        ExpressOrderBean response = new ExpressOrderBean();
    	BeanUtils.copyProperties(response, resp);
		return Response.success(req, response);
    }

    @ApiOperation(value = "省市区地址合并拉单接口", notes = "客户端发起请求后，圆通电子面单接口在http response里把电子面单信息同步给客户端。")
    @RequestMapping(value = "/e-ord-apply", method = {RequestMethod.POST}, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Response<MianResponse> eOrdApply(@RequestBody @Valid MianRequestOrderDTO req) {
        return Response.success(req, ytoAppService.eOrdApply(req));
    }

    @ApiOperation(value = "走件流程查询接口", notes = "根据圆通物流运单号查询已有的快件物流信息，在物流信息里面会包含物流状态，如 【客户 **** 已签收】，物流信息保持与官网一致。")
    @RequestMapping(value = "/waybill-query", method = {RequestMethod.POST}, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Response<WaybillQueryResponse> waybillQuery(@RequestBody @Valid WaybillQueryReqDTO req) {
        return Response.success(req, ytoAppService.waybillQuery(req));
    }

    @ApiOperation(value = "标准运价查询接口", notes = "根据始发地省市、目的地省市等信息，查询此次快递所需费用（官方标准版）。查询到的运价收费以当地网点为准，此接口返回的运价仅作参考。")
    @RequestMapping(value = "/charge-query", method = {RequestMethod.POST}, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Response<ChargeQueryResponse> chargeQuery(@RequestBody @Valid TransportInfoReqDTO req) {
        return Response.success(req, ytoAppService.chargeQuery(req));
    }

}
